
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Component," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>自定义包裹组件的HTML标签 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        自定义包裹组件的HTML标签
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Component/" rel="tag">Ember-Component</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 5 分钟</span>
    </header>

    <div class="post-content">
      <p>按照惯例，先做好准备工作，使用<a target="_blank" rel="noopener" href="http://ember-cli.com/user-guide">Ember CLI</a>命令生成演示所需的文件：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ember g route customizing-component-element</span><br><span class="line">ember g component customizing-component-element</span><br><span class="line">ember g route home</span><br><span class="line">ember g route about</span><br></pre></td></tr></table></figure>
<p>默认情况下，组件会被包裹在<code>div</code>标签内。比如，组件渲染之后得到下面的代码：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;ember180&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ember-view&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">h1</span>&gt;</span>My Component<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><code>h1</code>标签就是组件的内容。以<code>ember</code>开头的<code>id</code>和<code>class</code>都是<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>自动生成的。如果你需要修改渲染之后生成的HTML不是被包裹在<code>div</code>标签，或者修改<code>id</code>和<code>class</code>等属性值为自定义的值，你可以在组件类中设置。</p>
<h2 id="1，自定义包裹组件的HTML标签"><a href="#1，自定义包裹组件的HTML标签" class="headerlink" title="1，自定义包裹组件的HTML标签"></a>1，自定义包裹组件的HTML标签</h2><p>默认情况下，组件会被包裹在<code>div</code>标签内，如果你需要修改这个默认值你可以在组件类中指定这个包裹的HTML标签。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/components/customizing-component-element.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// 使用tabName属性指定渲染之后HTML标签</span></span><br><span class="line">    <span class="comment">// 注意属性的值必须是标准的HTML标签名</span></span><br><span class="line">    tagName: <span class="string">&#x27;nav&#x27;</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>下面自定义一个组件。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/components/customizing-component-element.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;&#123;#link-to &#x27;home&#x27;&#125;&#125;Home&#123;&#123;/link-to&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">li</span>&gt;</span>&#123;&#123;#link-to &#x27;about&#x27;&#125;&#125;About&#123;&#123;/link-to&#125;&#125;<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>下面是调用组件的模板代码。注意组件被包裹在那个HTML标签内，正确情况下应该是被包裹在<code>nav</code>标签中。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/customizing-component-element.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line">&#123;&#123;customizing-component-element&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>页面加载之后查看页面的源代码。如下：</p>
<p><img src="http://static.oschina.net/uploads/img/201510/12021703_xHgu.png"></p>
<p>可以看到组件<code>customizing-component-element</code>的内容确实是被包裹在<code>nav</code>标签之中，如果在组件类中没有使用属性<code>tagName</code>指定包裹的HTML标签，默认是<code>div</code>，你可以把组件类中<code>tagName</code>属性删除之后再查看页面的HTML源码代码。</p>
<h2 id="2，自定义包裹组件的HTML元素的类名"><a href="#2，自定义包裹组件的HTML元素的类名" class="headerlink" title="2，自定义包裹组件的HTML元素的类名"></a>2，自定义包裹组件的HTML元素的类名</h2><p>默认情况下，<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>会自动为包裹组件的HTML元素增加一个以<code>ember</code>开头的类名，如果你需要增加自定义的CSS类，可以在组件类中使用<code>className</code>数组属性指定，可以一次性指定多个CSS类。比如下面的代码例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/components/customizing-component-element.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// 使用tabName属性指定渲染之后HTML标签</span></span><br><span class="line">    <span class="comment">// 注意属性的值必须是标准的HTML标签名</span></span><br><span class="line">    tagName: <span class="string">&#x27;nav&#x27;</span>,</span><br><span class="line">    classNames: [<span class="string">&#x27;primary&#x27;</span>, <span class="string">&#x27;my-class-name&#x27;</span>]  <span class="comment">//指定包裹元素的CSS类</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>页面重新加载之后查看源代码，可以看到<code>nav</code>标签中多了两个CSS类，一个是<code>primary</code>，一个是<code>my-class-name</code>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">id</span>=<span class="string">&quot;ember411&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ember-view primary my-class-name&quot;</span>&gt;</span></span><br><span class="line">⋯⋯</span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>如果你想根据某个数据的值决定是否增加CSS类也是可以做到的，比如下面的代码，当<code>urgent</code>为<code>true</code>的时增加一个CSS类<code>urgent</code>，否则不增加这个类。要达到这个目的可以通过属性<code>classNameBindings</code>设置。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/components/customizing-component-element.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// 使用tabName属性指定渲染之后HTML标签</span></span><br><span class="line">    <span class="comment">// 注意属性的值必须是标准的HTML标签名</span></span><br><span class="line">    tagName: <span class="string">&#x27;nav&#x27;</span>,</span><br><span class="line">    classNames: [<span class="string">&#x27;primary&#x27;</span>, <span class="string">&#x27;my-class-name&#x27;</span>],  <span class="comment">//指定包裹元素的CSS类</span></span><br><span class="line">    classNameBindings: [<span class="string">&#x27;urgent&#x27;</span>],</span><br><span class="line">    urgent: <span class="literal">true</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>页面重新加载之后查看源代码，可以看到<code>nav</code>标签中多了一个CSS类<code>urgent</code>，如果属性<code>urgent</code>的值为<code>false</code>，CSS类<code>urgent</code>将不会渲染到<code>nav</code>标签上。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">id</span>=<span class="string">&quot;ember411&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ember-view primary my-class-name urgent&quot;</span>&gt;</span></span><br><span class="line">⋯⋯</span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>：<code>classNameBindings</code>指定的属性值必须要跟用于判断数据的属性名一致，比如这个例子中<code>classNameBindings</code>指定的属性值是<code>urgent</code>，用户判断是否增加类的属性也是<code>urgent</code>。如果这个属性只是驼峰式命名的那么渲染之后CSS类名将是以中划线<code>-</code>分隔，比如<code>classNameBindings</code>指定一个名为<code>secondClassName</code>，渲染后的CSS类为<code>second-class-name</code>。比如下面的演示代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/components/customizing-component-element.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// 使用tabName属性指定渲染之后HTML标签</span></span><br><span class="line">    <span class="comment">// 注意属性的值必须是标准的HTML标签名</span></span><br><span class="line">    tagName: <span class="string">&#x27;nav&#x27;</span>,</span><br><span class="line">    classNames: [<span class="string">&#x27;primary&#x27;</span>, <span class="string">&#x27;my-class-name&#x27;</span>],  <span class="comment">//指定包裹元素的CSS类</span></span><br><span class="line">    classNameBindings: [<span class="string">&#x27;urgent&#x27;</span>, <span class="string">&#x27;secondClassName&#x27;</span>],</span><br><span class="line">    urgent: <span class="literal">true</span>,</span><br><span class="line">    secondClassName: <span class="literal">true</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>页面重新加载之后查看源代码，可以看到<code>nav</code>标签中多了一个CSS类<code>second-class-name</code>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">id</span>=<span class="string">&quot;ember411&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ember-view primary my-class-name urgent second-class-name&quot;</span>&gt;</span></span><br><span class="line">⋯⋯</span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>如果你不想渲染之后的CSS类名被修改为中划线分隔形式，你可以值<code>classNameBindings</code>属性中指定渲染之后的CSS类名。比如下面的代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/components/customizing-component-element.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// 使用tabName属性指定渲染之后HTML标签</span></span><br><span class="line">    <span class="comment">// 注意属性的值必须是标准的HTML标签名</span></span><br><span class="line">    tagName: <span class="string">&#x27;nav&#x27;</span>,</span><br><span class="line">    classNames: [<span class="string">&#x27;primary&#x27;</span>, <span class="string">&#x27;my-class-name&#x27;</span>],  <span class="comment">//指定包裹元素的CSS类</span></span><br><span class="line">    classNameBindings: [<span class="string">&#x27;urgent&#x27;</span>, <span class="string">&#x27;secondClassName:scn&#x27;</span>],  <span class="comment">//指定secondClassName渲染之后的CSS类名为scn</span></span><br><span class="line">    urgent: <span class="literal">true</span>,</span><br><span class="line">    secondClassName: <span class="literal">true</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>页面重新加载之后查看源代码，可以看到<code>nav</code>标签中原来CSS类为<code>second-class-name</code>的变成了<code>scn</code>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">id</span>=<span class="string">&quot;ember411&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ember-view primary my-class-name urgent scn&quot;</span>&gt;</span></span><br><span class="line">⋯⋯</span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>有没有感觉<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>既灵活又强大！！<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>的设计理念是“约定优于配置”！所以很多的属性默认的设置都是我们平常开发中最常用的格式。</p>
<p>除了上述可以指定CSS类名之外，还可以在<code>classNameBindings</code>增加简单的逻辑，特别是在处理一些动态效果的时候上述特性是非常有用的。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/components/customizing-component-element.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// 使用tabName属性指定渲染之后HTML标签</span></span><br><span class="line">    <span class="comment">// 注意属性的值必须是标准的HTML标签名</span></span><br><span class="line">    tagName: <span class="string">&#x27;nav&#x27;</span>,</span><br><span class="line">    classNames: [<span class="string">&#x27;primary&#x27;</span>, <span class="string">&#x27;my-class-name&#x27;</span>],  <span class="comment">//指定包裹元素的CSS类</span></span><br><span class="line">    classNameBindings: [<span class="string">&#x27;urgent&#x27;</span>, <span class="string">&#x27;secondClassName:scn&#x27;</span>, <span class="string">&#x27;isEnabled:enabled:disabled&#x27;</span>],</span><br><span class="line">    urgent: <span class="literal">true</span>,</span><br><span class="line">    secondClassName: <span class="literal">true</span>,</span><br><span class="line">    isEnabled: <span class="literal">true</span>  <span class="comment">//如果这个属性为true，类enabled将被渲染到nav标签上，如果属性值为false类disabled将被渲染到nav标签上，类似于三目运算</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>正如代码的注释所说的，<code>isEnabled:enabled:disabled</code>可以理解为一个三目运算，会根据<code>isEnabled</code>的值渲染不同的CSS类到<code>nav</code>上。</p>
<p>下面的HTML代码是<code>isEnabled</code>为<code>true</code>的情况，对于<code>isEnabled</code>为<code>false</code>的情况请读者自己试试：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">id</span>=<span class="string">&quot;ember411&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ember-view primary my-class-name urgent scn enabled&quot;</span>&gt;</span></span><br><span class="line">⋯⋯</span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p><strong>注意</strong>：如果用于判断的属性值不是一个<code>Boolean</code>值而是一个字符串那么得到的结果与上面的结果是不一样的，<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>会直接把这个字符串的值作为CSS类名渲染到包裹的标签上。比如下面的代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/components/customizing-component-element.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    <span class="comment">// 使用tabName属性指定渲染之后HTML标签</span></span><br><span class="line">    <span class="comment">// 注意属性的值必须是标准的HTML标签名</span></span><br><span class="line">    tagName: <span class="string">&#x27;nav&#x27;</span>,</span><br><span class="line">    classNames: [<span class="string">&#x27;primary&#x27;</span>, <span class="string">&#x27;my-class-name&#x27;</span>],  <span class="comment">//指定包裹元素的CSS类</span></span><br><span class="line">    classNameBindings: [<span class="string">&#x27;urgent&#x27;</span>, <span class="string">&#x27;secondClassName:scn&#x27;</span>, <span class="string">&#x27;isEnabled:enabled:disabled&#x27;</span>, <span class="string">&#x27;stringValue&#x27;</span>],</span><br><span class="line">    urgent: <span class="literal">true</span>,</span><br><span class="line">    secondClassName: <span class="literal">true</span>,</span><br><span class="line">    isEnabled: <span class="literal">true</span>,  <span class="comment">//如果这个属性为true，类enabled将被渲染到nav标签上，如果属性值为false类disabled将被渲染到nav标签上，类似于三目运算</span></span><br><span class="line">    stringValue: <span class="string">&#x27;renderedClassName&#x27;</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>此时页面的HTML源码就有点不一样了。<code>renderedClassName</code>作为CSS类名被渲染到了<code>nav</code>标签上。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">id</span>=<span class="string">&quot;ember411&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ember-view primary my-class-name urgent scn enabled renderedClassName&quot;</span>&gt;</span></span><br><span class="line">⋯⋯</span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>对于这点需要特别注意。<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>对于<code>Boolean</code>值和其他值的判断结果是不一样的。</p>
<h2 id="3，自定义包裹组件的HTML元素的属性"><a href="#3，自定义包裹组件的HTML元素的属性" class="headerlink" title="3，自定义包裹组件的HTML元素的属性"></a>3，自定义包裹组件的HTML元素的属性</h2><p>在前面两点介绍了包裹组件的HTML元素的标签名、CSS类名，在HTML标签上出来CSS类另外一个最常用的就是属性，那么<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>同样提供了自定义包裹HTML元素的属性的方法。使用<code>attributeBindings</code>属性指定，这个属性的属性方式与<code>classNameBindings</code>基本一致。<br>为了与前面的例子区别开新建一个组件<code>link-items</code>，使用命令<code>ember g component link-items</code>创建。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/components/link-items.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line">这是个组件</span><br></pre></td></tr></table></figure>
<p>在模板中调用组件。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/customizing-component-element.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line">&#123;&#123;customizing-component-element&#125;&#125;</span><br><span class="line"><span class="tag">&lt;<span class="name">br</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"></span><br><span class="line">&#123;&#123;link-items&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>下面设置组件类，指定包裹的HTML标签为<code>a</code>标签，并增加一个属性<code>href</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/components/link-items.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Component.extend(&#123;</span><br><span class="line">    tagName: <span class="string">&#x27;a&#x27;</span>,</span><br><span class="line">    attributeBindings: [<span class="string">&#x27;href&#x27;</span>],</span><br><span class="line">    href: <span class="string">&#x27;http://www.google.com.hk&#x27;</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>页面重新加载之后得到如下结果：</p>
<p><img src="http://static.oschina.net/uploads/img/201510/12021703_cRJd.png"></p>
<p>比较简单，对于渲染之后的结果我就不过多解释了，请参考<code>classNameBindings</code>属性理解。</p>
<p>到此，有关于组件渲染之后包裹组件的HTML标签的相关设置介绍完毕。内容不多，<code>classNameBindings</code>和<code>attributeBindings</code>这两个属性的使用方式基本相同。如有疑问欢迎给我留言或者直接查看官方教程。<br><br><br><br><br>博文完整代码放在<a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/my_emberjs_code">Github</a>（博文经过多次修改，博文上的代码与github代码可能有出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个<code>star</code>吧。您的肯定对我来说是最大的动力！！</p>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/controllers/%E6%8E%A7%E5%88%B6%E5%99%A8%EF%BC%88controller%EF%BC%89/" rel="next" title="控制器（controller）">
          控制器（controller）
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/components/%E7%BB%84%E4%BB%B6%E5%AE%9A%E4%B9%89/" rel="prev" title="组件定义">
            组件定义
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1%EF%BC%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8C%85%E8%A3%B9%E7%BB%84%E4%BB%B6%E7%9A%84HTML%E6%A0%87%E7%AD%BE"><span class="toc-text">1，自定义包裹组件的HTML标签</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2%EF%BC%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8C%85%E8%A3%B9%E7%BB%84%E4%BB%B6%E7%9A%84HTML%E5%85%83%E7%B4%A0%E7%9A%84%E7%B1%BB%E5%90%8D"><span class="toc-text">2，自定义包裹组件的HTML元素的类名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3%EF%BC%8C%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8C%85%E8%A3%B9%E7%BB%84%E4%BB%B6%E7%9A%84HTML%E5%85%83%E7%B4%A0%E7%9A%84%E5%B1%9E%E6%80%A7"><span class="toc-text">3，自定义包裹组件的HTML元素的属性</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '自定义包裹组件的HTML标签', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
